//
//  BaseViewController.m
//  WindRunner
//
//  Created by leeqb on 9/10/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "BaseViewController.h"
#import "BaseTableViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

+ (void)initViewController:(id)obj
{
    UIViewController *vc = (UIViewController *)obj;
    //vc.view.backgroundColor = BACKGROUND_COLOR; // 导航栏背景颜色
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]; // 导航栏标题颜色
    
    // 设置导航条颜色
    if([vc.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
        vc.navigationController.navigationBar.barTintColor = THEME_COLOR;
    } else {
        vc.navigationController.navigationBar.tintColor = THEME_COLOR;
    }
    
    // 设置TabBar颜色
    if([vc.tabBarController.tabBar respondsToSelector:@selector(setBarTintColor:)]) {
        vc.tabBarController.tabBar.barTintColor = THEME_COLOR;
    } else {
        vc.tabBarController.tabBar.tintColor = THEME_COLOR;
    }
    
    if(!vc.navigationItem.hidesBackButton) {
        UIImage *backImage = [UIImage imageNamed:@"nav_back.png"];
        CGRect rect = CGRectZero;
        rect.size.width = backImage.size.width / 2;
        rect.size.height = backImage.size.height / 2;
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = rect;
        [backButton setImage:backImage forState:UIControlStateNormal];
        [backButton addTarget:vc action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, 6, 0, -6)];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        vc.navigationItem.leftBarButtonItem = backItem;
    }
}

+ (void)navBack:(id)obj
{
    UIViewController *vc = (UIViewController *)obj;
    [vc.navigationController popViewControllerAnimated:YES];
}

- (void)loadView
{
    [super loadView];
    [BaseViewController initViewController:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:NSStringFromClass([self class])];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:NSStringFromClass([self class])];
}

// 返回上一层
- (void)backButtonClicked
{
    [BaseViewController navBack:self];
}

@end
